﻿// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
//
// 本项目主要遵循 MIT 许可证和 Apache 许可证（版本 2.0）进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
//
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动！任何基于本项目二次开发而产生的一切法律纠纷和责任，我们不承担任何责任！

using System.ComponentModel;

namespace Admin.NET.Application.Service.PlanViews.Dto;

/// <summary>
/// 周期分析 - 通用过滤
/// </summary>
public sealed class CycleCommonFilters
{
    public string? WorkTime { get; set; }            // 业务月：yyyy-MM（默认使用业务月起止）
    public DateTime? RangeStart { get; set; }        // 自定义起止（优先）
    public DateTime? RangeEnd { get; set; }

    public long? ClassName { get; set; }             // 分类Id
    public long? WorkAreaId { get; set; }            // 区域Id

    public List<string>? ShipNos { get; set; }       // 船号集合（Ch）
    public List<string>? Sections { get; set; }      // 分段集合（Fd）
    public string? Keyword { get; set; }             // 关键字（Ch/Fd/Team/Remark）

    public bool? OnlyCompleted { get; set; }         // 仅完结（ActualEnd != null）
    public bool? IgnoreNegative { get; set; }        // 忽略负周期（end<start）
}

/// <summary>
/// 时间匹配口径
/// </summary>
public enum TimeMatchMode
{
    Intersects = 0,      // 区间相交（默认）
    StartWithin = 1,     // 起点在区间内
    EndWithin = 2        // 终点在区间内
}

/// <summary>
/// 周期分析 - 时间口径
/// </summary>
public enum TimeFieldMode
{
    Both = 0,            // 计划或实际任一满足（默认）
    Plan = 1,
    Actual = 2
}

/// <summary>
/// 树形分析请求
/// </summary>
public class CycleTreeRequest
{
    public CycleCommonFilters Filters { get; set; } = new();
    public TimeFieldMode TimeField { get; set; } = TimeFieldMode.Both;
    public TimeMatchMode TimeMatch { get; set; } = TimeMatchMode.Intersects;
    public List<string> Dims { get; set; } = new();      // 例如: ["ship","section","category","region"]

    // 懒加载与分页（根层）
    public bool Lazy { get; set; } = false;
    public List<string>? ParentKeyPath { get; set; }     // 懒加载：父路径
    public int PageIndex { get; set; } = 1;
    public int PageSize { get; set; } = 50;

    // 排序（可选）
    public List<SortSpec>? Sort { get; set; }
}

/// <summary>
/// 排序规格
/// </summary>
public sealed class SortSpec
{
    public string Field { get; set; } = "";  // 例如: actualCycleAvgDays
    public bool Desc { get; set; } = false;
}

/// <summary>
/// 树形节点
/// </summary>
public sealed class CycleTreeNode
{
    public string Id { get; set; } = string.Empty;      // keyPath 拼接
    public List<string> KeyPath { get; set; } = new();
    public int Level { get; set; }
    public Dictionary<string, string?> Labels { get; set; } = new(); // 维度显示名

    // 数量
    public int ItemCount { get; set; }
    public int CompletedCount { get; set; }
    public int InProgressCount { get; set; }

    // 周期与跨度（天）
    public double? PlanCycleAvgDays { get; set; }
    public double? ActualCycleAvgDays { get; set; }
    public double? VarianceAvgDays { get; set; }
    public double? PlanSpanDays { get; set; }
    public double? ActualSpanDays { get; set; }
    public double? VarianceSumDays { get; set; }

    public bool HasChildren { get; set; }
    public List<CycleTreeNode>? Children { get; set; }

    // 内部累加项（父层计算用）
    public double PlanCycleSumDays { get; set; }
    public int PlanCycleCount { get; set; }
    public double ActualCycleSumDays { get; set; }
    public int ActualCycleCount { get; set; }
    public DateTime? MinPlanStart { get; set; }
    public DateTime? MaxPlanEnd { get; set; }
    public DateTime? MinActualStart { get; set; }
    public DateTime? MaxActualEnd { get; set; }
}

/// <summary>
/// 树形响应
/// </summary>
public sealed class CycleTreeResponse
{
    public List<CycleTreeNode> Nodes { get; set; } = new();
    public int Total { get; set; }
    public int PageIndex { get; set; }
    public int PageSize { get; set; }
}

/// <summary>
/// 透视表请求
/// </summary>
public sealed class CyclePivotRequest
{
    public CycleCommonFilters Filters { get; set; } = new();
    public TimeFieldMode TimeField { get; set; } = TimeFieldMode.Both;
    public TimeMatchMode TimeMatch { get; set; } = TimeMatchMode.Intersects;

    public List<string> RowDims { get; set; } = new();
    public List<string> ColDims { get; set; } = new();

    public bool ShowTotals { get; set; } = true;
}

/// <summary>
/// 透视表头
/// </summary>
public sealed class PivotHeader
{
    public List<string> KeyPath { get; set; } = new(); // 维度组合键
    public string Label { get; set; } = string.Empty;   // 展示名（最后一级）
    public int Level { get; set; }
}

/// <summary>
/// 透视单元格
/// </summary>
public sealed class PivotCell
{
    public int R { get; set; }                  // 行索引
    public int C { get; set; }                  // 列索引

    // 指标
    public int ItemCount { get; set; }
    public double? PlanCycleAvgDays { get; set; }
    public double? ActualCycleAvgDays { get; set; }
    public double? PlanCycleSumDays { get; set; }
    public double? ActualCycleSumDays { get; set; }
    public double? VarianceSumDays { get; set; }
    public double? VarianceAvgDays { get; set; }
}

/// <summary>
/// 透视响应
/// </summary>
public sealed class CyclePivotResponse
{
    public List<PivotHeader> Rows { get; set; } = new();
    public List<PivotHeader> Cols { get; set; } = new();
    public List<PivotCell> Cells { get; set; } = new();
}

/// <summary>
/// 树形导出请求
/// </summary>
public sealed class CycleTreeExportRequest : CycleTreeRequest
{
    /// <summary>
    /// 导出全部（当前层全部，忽略分页）；false 仅导出当前页
    /// </summary>
    public bool ExportAll { get; set; } = false;

    /// <summary>
    /// 导出全部层级（递归导出整棵树/子树）
    /// </summary>
    public bool ExportAllLevels { get; set; } = false;
}
